<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Logic\Api\UploadLogic;
use App\Http\Requests\Api\Upload\Create;
use App\Http\Requests\Api\Upload\IdCard;
use App\Http\Requests\Request;
use Exception;
use Illuminate\Http\JsonResponse;

class UploadController extends Controller
{
    protected UploadLogic $uploadLogic;

    public function __construct(UploadLogic $uploadLogic)
    {
        $this->uploadLogic = $uploadLogic;
    }

    public function create(Create $create): JsonResponse
    {
        try {
            $params = $create->validated();

            return $this->success(
                $this->uploadLogic->upload($params['file'])
            );
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function delete(Request $request)
    {
        try{
            //删除文件
            $params = $request->all();
            return $this->success(
                $this->uploadLogic->delete($params['path'])
            );
        }catch (Exception $e){
            return $this->error($e->getMessage());
        }
    }

    public function idCard(IdCard $idCard): JsonResponse
    {
        try {
            $params = $idCard->validated();

            return $this->success(
                $this->uploadLogic->idCard($params['file'])
            );
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
